﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.IO;
using System.Threading;
using System.Text;
using System.Net;
using UnityEngine;
using HtmlAgilityPack;

public class ArtistDownloader : MonoBehaviour
{
    public ArtistInfo artistInfo;
    public AppController appController;
    Texture2D tex2d;

    public void Print()
    {
        Debug.Log("*************************************************");
        Debug.Log(this);
        Debug.Log("Name: " + artistInfo.artistName);
        Debug.Log("Aliases: " + artistInfo.artistAliases);
        Debug.Log("Page alias: " + artistInfo.pageAlias);
        Debug.Log("Page ID: " + artistInfo.pageID);
    }
    public void GetLinksToImages()
    {
        //Debug.Log("Get link images start");
        WebRequest request;
        WebResponse response;
        Stream artscrollDataStream;
        HtmlDocument artscrollHTMLDoc = new HtmlDocument();

        request = WebRequest.Create(artistInfo.fullAliasLink);
        request.Credentials = CredentialCache.DefaultCredentials;

        //Console.WriteLine("Getting response...");
        response = request.GetResponse();
        Console.WriteLine("Status: " + ((HttpWebResponse)response).StatusDescription);

        artscrollDataStream = response.GetResponseStream();
        artscrollHTMLDoc.Load(artscrollDataStream);

        var tableNode = artscrollHTMLDoc.GetElementbyId("ArtWorksTable");

        if (tableNode == null)
        {
            Debug.Log("tableNode == null");
            return;
        }     
        
        var trNodes = tableNode.ChildNodes;

        foreach (HtmlNode node in trNodes)
        {
            if (node.Name == "tr")
            {
                var tmp = node.Descendants();
                foreach (HtmlNode tmp_node in tmp)
                    if (tmp_node.Name == "a")
                        if (tmp_node.Attributes["href"].Value.Contains(".jpg"))
                        {
                            (appController.artistsListRaw[artistInfo.artistName[0]][artistInfo.artistName]as ArtistInfo).linksToImages.Add(ArtistInfo.artscrollBaseHost + tmp_node.Attributes["href"].Value);
                            (appController.artistsListRaw[artistInfo.artistName[0]][artistInfo.artistName]as ArtistInfo).imagesCache.Add(false);
                        }
            }
        }
        (appController.artistsListRaw[artistInfo.artistName[0]][artistInfo.artistName]as ArtistInfo).linkToImagesCache = true;
        //Debug.Log("Get link images end");
    }

    /*
    public IEnumerator WaitForImage(String URL)
    {
        WWW www = new WWW(URL);
        yield return www;
        if (www.texture != null)
        {
            Debug.Log("www.texure != null");
            (appController.artistsListRaw[artistInfo.artistName[0]][artistInfo.artistName] as ArtistInfo).images.Add(www.texture);
            appController.imagesGridController.AddCell((appController.artistsListRaw[artistInfo.artistName[0]][artistInfo.artistName] as ArtistInfo), 
                                                        (appController.artistsListRaw[artistInfo.artistName[0]][artistInfo.artistName] as ArtistInfo).images.Count - 1);
        }
    }

    IEnumerator DownloadImage(String URL)
    {
        Debug.Log("Start download " + URL);
        yield return WaitForImage(URL);
        Debug.Log("Download complete " + URL);
    }

    public IEnumerator DownloadAllImages()
    {
        Debug.Log("Start all downloads");
        while (true)
        {
            for (int i = 0; i < artistInfo.linksToImages.Count; i++)
            {
                Debug.Log("     Start download image");
                yield return WaitForImage(artistInfo.linksToImages[i]);
                Debug.Log("     Stop download image");
            }
        }
        Debug.Log("All downloads is complete");
    }
     **/
}
